package restless.core.gameObjs {
	
	import flash.geom.Point;
	import restless.aimovement.astar.interfaces.IMap;
	import restless.aimovement.astar.Map;
	import restless.core.managers.RLERender;
	import restless.utilities.numeric.RLERandom;

	
	/**
	 * ...
	 * @author Jordan Laine
	 */
	public class RLEMovingGridObj extends RLEGridObj {
		
		private var _map:IMap;
		private var bKeyboard:Boolean = false;
		
		protected var nLastDirection:int;
		
		protected var nLastCol:int;
		protected var nNextCol:int;
		protected var nLastRow:int;
		protected var nNextRow:int;
		
		protected var bMoveDelay:Boolean = false;
		protected var nMoveDelay:int = 0;
		protected var nMoveCounter:int = 0;
		
		protected var nCurrentDirection:int;
		protected var nextX:Number;
		protected var nextY:Number;
		
		private var nMoveSpeed:Number;
		
		protected const STOP:int = 0;
		protected const UP:int = 1;
		protected const DOWN:int = 2;
		protected const LEFT:int = 3;
		protected const RIGHT:int = 4;
		
		private var tempRow:Number;
		private var tempCol:Number;
		private var tempDir:int;
		
		
		public function getNextCol():int {
			return this.nNextCol;
		}
		
		public function getNextRow():int {
			return this.nNextRow;
		}
		
		public function RLEMovingGridObj() {
			
		}
		
		public function initMovingSpriteData(moveDir:int, col:int, row:int, width:int, height:int):void {
			gridPosition(col, row);
			gridCellSize(width, height);
			
			nCurrentDirection = moveDir;		
			switchMovement(nCurrentDirection);
			checkNewSquare();
		}
		
		//calculates to see if the obj is in a new square
		protected function checkNewSquare():Boolean {
			
			nextX = x + (dx * nMoveSpeed);
			nextY = y + (dy * nMoveSpeed);
			
			if (nextX == 0) {
				trace("Next X = 0, WTF?");
			}
			tempCol = Math.round(nextX) / tileWidth;
			tempRow = Math.round(nextY) / tileHeight;
			
			if (tempCol != col || tempRow != row) {
				nLastRow = row;
				nLastCol = col;
				
				gridPosition(tempCol, tempRow);
				
				nNextRow = row + dy;
				nNextCol = col + dx;
				
				return true;
			} else {
				return false;
			}
		}
		
		public override function update():void {
			x = nextX;
			y = nextY;
			super.update();
		}
		
		protected function checkTile(dir:int):Boolean {
			checkMovement(dir);
			return map.getTileAt(tempPoint).getWalkable(tempPoint.x, tempPoint.y)
		}
		
		protected function checkMovement(dir:int):Point {
			tempDir = dir;
			
			switch(tempDir) {
				case UP:
					dx = 0;
					dy = -1;
					break;
				
				case DOWN:
					dx = 0;
					dy = 1;
					break;
				
				case LEFT:
					dx = -1;
					dy = 0;
					break;
				
				case RIGHT:
					dx = 1;
					dy = 0;
					break;
				
				case STOP:
					dx = 0;
					dy = 0;
					break;
			}
			
			tempCol = col + dx;
			tempRow = row + dy;
			
			tempPoint.x = tempCol;
			tempPoint.y = tempRow;
			
			return tempPoint;
		}
		
		protected function switchMovement(direction:int):void {
			
			nLastDirection = nCurrentDirection;
			nCurrentDirection = direction;
			
			switch(direction) {
				case UP:
					rotation = 0;
					dx = 0;
					dy = -1;
					break;
				
				case DOWN:
					rotation = 180;
					dx = 0;
					dy = 1;
					break;
				
				case LEFT:
					rotation = -90;
					dx = -1;
					dy = 0;
					break;
				
				case RIGHT:
					rotation = 90;
					dx = 1;
					dy = 0;
					break;
				
				case STOP:
					dx = 0;
					dy = 0;
					break;
			}
			
			nNextRow = row + dy;
			nNextCol = col + dx;
			
		}
		
		protected function randomMoveVariation():Boolean {
			var randomBool:Boolean = RLERandom.randomBool();
			return randomBool;
		}
		
		public function set moveSpeed(num:Number):void {
			nMoveSpeed = num;
		}
		
		public function get moveSpeed():Number {
			return nMoveSpeed;
		}
		
		public function set map(map:IMap):void {
			_map = map;
		}
		
		public function get map():IMap {
			return _map;
		}
		
		protected function checkCenterTile():Boolean {
			tempPoint.x = (col * tileWidth) + (.5 * tileWidth);
			tempPoint.y = (row * tileHeight) + (.5 * tileHeight);
			
		if (x == tempPoint.x && y == tempPoint.y) {
				return true;
			}else {
				return false;
			}
		}
		
		override public function dispose():void {
			map = null;
			super.dispose();
		}
		
	}

}